/*
 * File: SendThankYousTest.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets.tests;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import static org.junit.Assert.*;
import javax.servlet.ServletException;

import org.junit.Test;

import servlets.cron.SendThankYous;
import tests.AppEngineTest;
import tests.HttpServletRequestMock;
import tests.HttpServletResponseMock;

import faceBook.Message;
import faceBook.User;
import faceBook.tests.helper.TestUsers;

/**
 * The Class SendThankYousTest.
 */
@SuppressWarnings("static-method")
public class SendThankYousTest extends AppEngineTest
{

  /**
   * Tests the sendThankYous method.
   * 
   * @throws ServletException
   *           the servlet exception
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  @Test
  public void testSendThankYous() throws ServletException, IOException
  {
    ArrayList<Message> user1Messages = Message.getAllMessagesForUser(TestUsers
        .getTestUser(1));
    ArrayList<Message> user2Messages = Message.getAllMessagesForUser(TestUsers
        .getTestUser(2));
    for (Message m : user1Messages)
    {
      m.delete();
    }
    for (Message m : user2Messages)
    {
      m.delete();
    }

    GregorianCalendar yesterday = new GregorianCalendar();
    yesterday.add(Calendar.DATE, -1);

    User test1 = TestUsers.getTestUser(1);
    User test2 = TestUsers.getTestUser(2);
    test1.setBirthday(yesterday.getTimeInMillis());
    test2.setBirthday(yesterday.getTimeInMillis());
    test1.setUseReply(true);
    test2.setUseReply(true);
    test1.setReplyMessage("Thanks *FIRST_NAME*---*LAST_NAME*");
    test2.setReplyMessage("Thanks *FIRST_NAME*---*LAST_NAME*");
    test1.save();
    test2.save();

    Message message = new Message();
    message.setUser(TestUsers.getTestUser(0));
    message.addFriend(TestUsers.getTestUser(1));
    message.setMessage("Happy Birthday");
    message.setLink("");
    message.postMessage();

    Message message2 = new Message();
    message2.setUser(TestUsers.getTestUser(0));
    message2.addFriend(TestUsers.getTestUser(2));
    message2.setMessage("Happy Bird-day");
    message2.setLink("");
    message2.postMessage();

    SendThankYous sty = new SendThankYous();
    sty.doGet(new HttpServletRequestMock(new HashMap<String, String>()),
        new HttpServletResponseMock());

    message.delete();
    message2.delete();

    user1Messages = Message.getAllMessagesForUser(TestUsers.getTestUser(1));
    for (Message m : user1Messages)
    {
      m.delete();
      assertEquals("Thanks New---Hack", m.getMessage());
    }
    user2Messages = Message.getAllMessagesForUser(TestUsers.getTestUser(2));
    assertTrue(user1Messages.size() > 0);
    assertTrue(user2Messages.size() == 0);
  }

}
